<?php

class OnlinePage extends Page {
	private $isOnline = true;

	public function __construct() {
		$this->isOnline = ServerStatistics::checkServerIsOnline();
	}

	public function writeHtmlHeader() {
		echo '<title>在线玩家'.STENDHAL_TITLE.'</title>';
		if (!$this->isOnline) {
			echo '<meta name="robots" content="noindex">'."\n";
		}
	}

	/**
	 * writes the page content.
	 */
	function writeContent() {
		if ($this->isOnline) {
			$this->writeOnlinePlayers();
		} else {
			$this->writeOfflineHint();
		}
		$this->writeRecent();
	}

	/**
	 * writes an informative text about the server being offline.
	 */
	function writeOfflineHint() {
		startBox("<h1>服务器离线</h1>");
		echo '<p>很抱歉！ 服务器现在离线.</p>';
		echo '<p>可能是服务器正在更新，也可能服务器出问题了.</p>';
		echo '<p>Please join our <a href="chat">MAIN_CHANNEL</a> for updates on the situation.</p>';
		endBox();
	}

	/**
	 * writes the list of online players.
	 */
	function writeOnlinePlayers() {
		$players = getOnlinePlayers();
		startBox('<h1>在线玩家</h1>');

		if(sizeof($players)==0) {
			echo '目前没有任何玩家登录.';
		}
		echo '<div class="tableCell cards">';
		foreach($players as $p) {
			echo '<div class="onlinePlayer onlinePlayerHeight player">';
			echo '  <a class = "onlineLink" href="'.rewriteURL('/character/'.urlencode($p->name).'.html').'">';
			echo '  <img src="'.rewriteURL('/images/outfit/'.surlencode($p->outfit).'.png').'" alt="" width="48" height="64">';
			echo '  <span class="block onlinename">'.htmlspecialchars($p->name).'</span></a>';
			echo '</div>';
		}
		echo '</div>';
		endBox();
	}

	function writeRecent() {
		$stats = ServerStatistics::readOnlineStats();
		$acc7 = isset($stats['accounts_7']) ? $stats['accounts_7'] : '0';
		$acc30 = isset($stats['accounts_30']) ? $stats['accounts_30'] : '0';
		$char7 = isset($stats['characters_7']) ? $stats['characters_7'] : '0';
		$char30 = isset($stats['characters_30']) ? $stats['characters_30'] : '0';
		startBox('<h2>最近在线</h2>');
		echo '<p>下表显示最近在线的帐户和角色的数量.</p>';
		echo '<table class="prettytable">';
		echo '<tr><th>&nbsp;</th><th>帐户</th><th>角色</th></tr>';
		echo '<tr><td>周</td><td>'.htmlspecialchars($acc7).'</td><td>'.htmlspecialchars($char7).'</td></tr>';
		echo '<tr><td>月</td><td>'.htmlspecialchars($acc30).'</td><td>'.htmlspecialchars($char30).'</td></tr>';
		echo '</table>';
		endBox();
	}
}

$page = new OnlinePage();
